﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace RIO.Utilits
{
  public static class ImageHelper
  {
    public static void ShowImage(Bitmap bmp)
    {
      //using (Form form = new Form())
      //{
        Form form = new Form();
          form.Width = bmp.Width;
        form.Height = bmp.Height;
        PictureBox pb = new PictureBox();
        pb.Parent = form;
        pb.Image = bmp;
        pb.Dock = DockStyle.Fill;
        pb.MouseMove += new MouseEventHandler(delegate(object sender, MouseEventArgs e)
          {
            try
            {
              form.Text = bmp.GetPixel(e.X, e.Y).ToString() +  " " + e.Location.ToString();
            }
            catch{};
          });

        form.ShowDialog();
    }

    public static bool CompareBitmap(Bitmap bmp1, Bitmap bmp2, Color transparent)
    {
      using (FastBitmap fbmp1 = new FastBitmap(bmp1),
                        fbmp2 = new FastBitmap(bmp2))
      {
        for (int i = 0; i < bmp1.Width; i++)
        {
          for (int j = 0; j < bmp1.Height; j++)
          {
            Color color = fbmp1.GetColor(i, j);
            //if (color.ToArgb() != transparent.ToArgb() && color.ToArgb() != fbmp2.GetColor(i, j).ToArgb())
            if (!CompareColor(color, transparent, 20) && !CompareColor(color, fbmp2.GetColor(i, j), 20))
            {
              return false;
            }
          }
        }
      }
      return true;
    }

    public static bool CompareColor(Color color1, Color color2, int delta)
    {
      if (Math.Abs(color1.R - color2.R) < delta &&
          Math.Abs(color1.G - color2.G) < delta &&
          Math.Abs(color1.B - color2.B) < delta)
      {
        return true;
      }

      return false;
    }
  }
}
